package com.peakmain.baselibrary.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;


import com.peakmain.baselibrary.R;

import java.util.ArrayList;

/**
 * author ：Peakmain
 * createTime：2019/4/2
 * mail:2726449200@qq.com
 * describe：多布局支持
 */
public class MultipleLayout extends RelativeLayout {
    public static final int NULL_RESOURCE_ID = -1;
    //五种状态
    public static final int STATUS_CONTENT = 0x00;
    public static final int STATUS_LOADING = 0x01;
    public static final int STATUS_EMPTY = 0x02;
    public static final int STATUS_ERROR = 0x03;
    public static final int STATUS_NO_NETWORK = 0x04;
    /**
     * 默认设置是全屏
     */
    private static final LayoutParams DEFAULT_LAYOUT_PARAMS =
            new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
    //view
    private View mEmptyView;
    private View mErrorView;
    private View mLoadingView;
    private View mNoNetworkView;
    private View mContentView;
    //布局的id
    private int mEmptyViewResId;
    private int mErrorViewResId;
    private int mLoadingViewResId;
    private int mNoNetworkViewResId;
    private int mContentViewResId;
    /**
     * 当前view的状态
     */
    private int mViewStatus;
    private LayoutInflater mInflater;
    //重试的点击事件
    private OnClickListener mOnRetryClickListener;

    private final ArrayList<Integer> mOtherIds = new ArrayList<>();

    public MultipleLayout(Context context) {
        this(context, null);
    }

    public MultipleLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MultipleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //解析自定义属性
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultipleStatusView, defStyleAttr, 0);
        mEmptyViewResId = a.getResourceId(R.styleable.MultipleStatusView_emptyView, R.layout.layout_empty_view);
        mErrorViewResId = a.getResourceId(R.styleable.MultipleStatusView_errorView, R.layout.layout_error_view);
        mLoadingViewResId = a.getResourceId(R.styleable.MultipleStatusView_loadingView, R.layout.layout_loading_view);
        mNoNetworkViewResId = a.getResourceId(R.styleable.MultipleStatusView_noNetworkView, R.layout.layout_network_view);
        mContentViewResId = a.getResourceId(R.styleable.MultipleStatusView_contentView, NULL_RESOURCE_ID);
        //回收
        a.recycle();
        mInflater = LayoutInflater.from(context);
    }


    /**
     * 自己设置没有数据的布局
     */

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        showContent();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        clear(mEmptyView, mLoadingView, mErrorView, mNoNetworkView);
        if (mOtherIds != null) {
            mOtherIds.clear();
        }
        if (mOnRetryClickListener != null) {
            mOnRetryClickListener = null;
        }
        mInflater = null;
    }

    /**
     * 获取当前状态
     */
    public int getViewStatus() {
        return mViewStatus;
    }

    /**
     * 设置重试点击事件
     *
     * @param onRetryClickListener 重试点击事件
     */
    public void setOnRetryClickListener(OnClickListener onRetryClickListener) {
        this.mOnRetryClickListener = onRetryClickListener;
    }

    /**
     * 显示空视图
     */
    public final void showEmptyView() {
        if (mEmptyView != null) {
            showEmpty(mEmptyView, DEFAULT_LAYOUT_PARAMS);
        } else {
            showEmpty(mEmptyViewResId, DEFAULT_LAYOUT_PARAMS);
        }
    }

    /**
     * 显示空视图
     *
     * @param layoutId     自定义布局文件
     * @param layoutParams 布局参数
     */
    private void showEmpty(int layoutId, LayoutParams layoutParams) {
        showEmpty(inflateView(layoutId), layoutParams);
    }

    /**
     * 显示空视图
     *
     * @param view         自定义视图
     * @param layoutParams 布局参数
     */
    public final void showEmpty(View view, ViewGroup.LayoutParams layoutParams) {
        checkNull(view, "EmptyView is null!");
        mViewStatus = STATUS_EMPTY;
        if (mEmptyView == null) {
            mEmptyView = view;
            View emptyRetryView = mEmptyView.findViewById(R.id.empty_retry_view);
            if (null != mOnRetryClickListener && null != emptyRetryView) {
                emptyRetryView.setOnClickListener(mOnRetryClickListener);
            }
            mOtherIds.add(mEmptyView.getId());
            addView(mEmptyView, 0, layoutParams);
        }
        showViewById(mEmptyView.getId());
    }

    /**
     * 显示错误视图
     */
    public final void showError() {
        if (mErrorView != null) {
            showError(mErrorView, DEFAULT_LAYOUT_PARAMS);
        } else {
            showError(mErrorViewResId, DEFAULT_LAYOUT_PARAMS);
        }
    }

    /**
     * 显示错误视图
     *
     * @param layoutId     自定义布局文件
     * @param layoutParams 布局参数
     */
    public final void showError(int layoutId, ViewGroup.LayoutParams layoutParams) {
        showError(inflateView(layoutId), layoutParams);
    }

    /**
     * 显示错误视图
     *
     * @param view         自定义视图
     * @param layoutParams 布局参数
     */
    public final void showError(View view, ViewGroup.LayoutParams layoutParams) {
        checkNull(view, "Error view is null!");
        mViewStatus = STATUS_ERROR;
        if (null == mErrorView) {
            mErrorView = view;
            View errorRetryView = mErrorView.findViewById(R.id.error_retry_view);
            if (null != mOnRetryClickListener && null != errorRetryView) {
                errorRetryView.setOnClickListener(mOnRetryClickListener);
            }
            mOtherIds.add(mErrorView.getId());
            addView(mErrorView, 0, layoutParams);
        }
        showViewById(mErrorView.getId());
    }

    /**
     * 显示加载中视图
     */
    public final void showLoading() {

        if (mLoadingView != null) {
            showLoading(mLoadingView, DEFAULT_LAYOUT_PARAMS);
        } else {
            showLoading(mLoadingViewResId, DEFAULT_LAYOUT_PARAMS);
        }
    }


    /**
     * 显示加载中视图
     *
     * @param layoutId     自定义布局文件
     * @param layoutParams 布局参数
     */
    public final void showLoading(int layoutId, ViewGroup.LayoutParams layoutParams) {
        showLoading(inflateView(layoutId), layoutParams);
    }

    /**
     * 显示加载中视图
     *
     * @param view         自定义视图
     * @param layoutParams 布局参数
     */
    public final void showLoading(View view, ViewGroup.LayoutParams layoutParams) {
        checkNull(view, "Loading view is null!");
        mViewStatus = STATUS_LOADING;
        if (null == mLoadingView) {
            mLoadingView = view;
            mOtherIds.add(mLoadingView.getId());
            addView(mLoadingView, 0, layoutParams);
        }
        showViewById(mLoadingView.getId());
    }

    /**
     * 显示无网络视图
     */
    public final void showNoNetwork() {
        if(mNoNetworkView!=null){
            showNoNetwork(mNoNetworkView, DEFAULT_LAYOUT_PARAMS);
        }else{
            showNoNetwork(mNoNetworkViewResId, DEFAULT_LAYOUT_PARAMS);
        }
    }

    /**
     * 显示无网络视图
     *
     * @param layoutId     自定义布局文件
     * @param layoutParams 布局参数
     */
    public final void showNoNetwork(int layoutId, ViewGroup.LayoutParams layoutParams) {
        showNoNetwork(inflateView(layoutId), layoutParams);
    }

    /**
     * 显示无网络视图
     *
     * @param view         自定义视图
     * @param layoutParams 布局参数
     */
    public final void showNoNetwork(View view, ViewGroup.LayoutParams layoutParams) {
        checkNull(view, "No network view is null!");
        mViewStatus = STATUS_NO_NETWORK;
        if (null == mNoNetworkView) {
            mNoNetworkView = view;
            View noNetworkRetryView = mNoNetworkView.findViewById(R.id.no_network_retry_view);
            if (null != mOnRetryClickListener && null != noNetworkRetryView) {
                noNetworkRetryView.setOnClickListener(mOnRetryClickListener);
            }
            mOtherIds.add(mNoNetworkView.getId());
            addView(mNoNetworkView, 0, layoutParams);
        }
        showViewById(mNoNetworkView.getId());
    }

    private void showViewById(int viewId) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            view.setVisibility(view.getId() == viewId ? View.VISIBLE : View.GONE);
        }
    }

    private View inflateView(int layoutId) {
        return mInflater.inflate(layoutId, null);
    }

    private void clear(View... views) {
        if (views == null) {
            return;
        }
        for (View view : views) {
            if (view != null) {
                removeView(view);
            }
        }
    }

    /**
     * 显示内容视图
     */
    public void showContent() {
        mViewStatus = STATUS_CONTENT;
        if (mContentView == null && mContentViewResId != NULL_RESOURCE_ID) {
            mContentView = mInflater.inflate(mContentViewResId, null);
            addView(mContentView, 0, DEFAULT_LAYOUT_PARAMS);
        }
        showContentView();
    }

    private void showContentView() {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            view.setVisibility(mOtherIds.contains(view.getId()) ? View.GONE : View.VISIBLE);
        }
    }

    private void checkNull(Object object, String hint) {
        if (null == object) {
            throw new NullPointerException(hint);
        }
    }

    /**
     * 设置自定的空view
     * @param emptyView 布局view
     */
    public void setEmptyView(View emptyView) {
        mEmptyView = emptyView;
    }

    /**
     * 设置自定的空view
     * @param emptyViewResId  R.layout.xx
     */
    public void setEmptyView(int emptyViewResId) {
        this.mEmptyViewResId = emptyViewResId;
    }

    /**
     * 设置自定义view的错误view
     * @param errorView 布局view
     */
    public void setErrorView(View errorView) {
        mErrorView = errorView;
    }
    /**
     * 设置自定义view的错误view
     * @param errorViewResId 布局view的id
     */
    public void setErrorView(int errorViewResId) {
        mErrorViewResId = errorViewResId;
    }
    /**
     * 设置自定义view的正在加载的view
     * @param loadingView 布局view
     */
    public void setLoadingView(View loadingView) {
        mLoadingView = loadingView;
    }
    /**
     * 设置自定义view的正在加载的view
     * @param loadingViewResId 布局view的id
     */
    public void setLoadingView(int loadingViewResId) {
        mLoadingViewResId = loadingViewResId;
    }

    /**
     * 设置自定义view的没有网络的view
     * @param noNetworkView 布局view
     */
    public void setNoNetworkView(View noNetworkView) {
        mNoNetworkView = noNetworkView;
    }
    /**
     * 设置自定义view的没有网络的view
     * @param noNetworkViewResId 布局view的id
     */
    public void setNoNetworkView(int noNetworkViewResId) {
        mNoNetworkViewResId = noNetworkViewResId;
    }

}
